var { getLoginedUser, setTitle, fnLoading, fnHideLoading, fnSuccess, fnFail } = require('../../utils/index')
import { domainStatic } from '../../utils/env.js';
import { checkLoginAsyc } from '../../utils/util.js';
import _ from '../../utils/underscore.js';
import { get, checkCoupon, fnChoose, lsGetStr, fnUpload, post } from '../../utils/request.js';
var WxParse = require('../../pages/wxParse/wxParse.js');
import { getType } from "../../utils/data_type.js";

Component({
  properties: {
    typeId:{
     type:String,
     value:'',
   }
  },
  data: {
    typeId:'',
    setdata:[],
    discountNam:'',
    cartItemId: "",
    cartItemnumber: "",
  },
  lifetimes: {
    attached: function () {
    },
    moved: function () {
    },
    detached: function () {
    },
  },
  attached() {
  }, // 此处attached的声明会被lifetimes字段中的声明覆盖
  ready: function () {
    this.setData({
      typeId: this.properties.typeId||''
    })
  },
  methods: {
    fnDetail(e) {
      var id = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: '../service-detail/index?id=' + id,
      });
    },
    //点击购物车
    cart: function (e) {
      var id = e.target.dataset.id;
      this.setData({
        cartItemId: id,
        cartItemnumber: 1,
      });
      checkLoginAsyc()
        .then(function () {
          post({
            path: "/order/addShoppingcart",
            data: {
              offerId: id,
              number: 1,
              skey: lsGetStr('skey')
            }
          })
        })
        .then(function (res) {
          fnSuccess("加入成功....")
        })
        .catch(e => {

        })
        ;
    },
    //点击购买
    aply: function (e) {
      var id = e.target.dataset.id;
      this.setData({
        cartItemId: id,
        cartItemnumber: 1,
      });
      checkLoginAsyc()
        .then(function () {
          post({
            path: "/order/addShoppingcart",
            data: {
              offerId: id,
              number: 1,
              skey: lsGetStr('skey'),
            }
          })
        })
        .then(function (res) {
          wx.switchTab({
            url: '../cart/index',
          })
        })
        .catch(e => {

        })
        ;

    },
  },

  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show: function () {
      let that = this;
      if (that.data.typeId == 14225) {
        fnLoading("加载中......")
        get({
          path: "/offer/query?type1=901"
        }).then(function (res) {
          fnHideLoading()
          var r = res.list.map(function (item) {
            item.unit = item.unit || '';
            return item;
          });
          that.setData({
            objBy: [],
            setdata: r
          })
        })
      } 
    },
  },
})